package com.ssy.lingxi.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Path转发定义
 *
 * @author 万宁
 * @version 2.0.0
 */
@SpringBootApplication
@Configuration
public class GatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    }


    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("system-service-route", r -> r.path("/system/**").filters(f -> f.stripPrefix(1)).uri("lb://system-service"))
                .route("file-service-route", r -> r.path("/file/**").filters(f -> f.stripPrefix(1)).uri("lb://file-service"))
                .route("merchant-member-service-route", r -> r.path("/member/**").uri("lb://merchant-member-service"))
                .route("workflow-service-route", r -> r.path("/workflow/**").uri("lb://workflow-service"))
                .route("platfrom-manage-service-route", r -> r.path("/manage/**").uri("lb://platform-manage-service"))
                .route("product-service-route", r -> r.path("/product/**").uri("lb://product-service"))
                .route("search-service-route", r -> r.path("/search/**").uri("lb://search-service"))
                .route("pay-service-route", r -> r.path("/pay/**").uri("lb://pay-service"))
                .route("logistics-service", r -> r.path("/logistics/**").uri("lb://logistics-service"))
                .route("order-service", r -> r.path("/order/**").uri("lb://order-service"))
                .route("platform-template-service", r -> r.path("/template/**").uri("lb://platform-template-service"))
                .route("settle-accounts-service", r -> r.path("/settle/accounts/**").uri("lb://settle-accounts-service"))
                .route("after-sale-service", r -> r.path("/as/**").uri("lb://after-sale-service"))
                .route("report-service", r -> r.path("/report/**").uri("lb://report-service"))
                .route("enhance-service", r -> r.path("/enhance/**").uri("lb://enhance-service"))
                .route("contract-service", r -> r.path("/contract/**").uri("lb://contract-service"))
                .route("transaction-service", r -> r.path("/transaction/**").uri("lb://transaction-service"))
                .route("purchase-service", r -> r.path("/purchase/**").uri("lb://purchase-service"))
                .route("message-service", r -> r.path("/message/**").uri("lb://message-service"))
                .route("marketing-service", r -> r.path("/marketing/**").uri("lb://marketing-service"))
                .build();
    }

//    /**
//     * 使用Resilience4j的默认配置进行熔断限流
//     *
//     * @return
//     */
//    @Bean
//    public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
//        return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
//                .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
//                .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()).build());
//    }
}
